package com.atwisdom.star.core.dsl.util;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.vo.ActionData;
import com.atwisdom.star.exception.pojo.BusinessException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.val;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DslServiceUtil {

    public static <T> QueryWrapper<T> buildOrderBy(QueryWrapper<T> queryWrapper,String orderBy,String defaultOrderBy ) {
        Map<String, List<String>> orderByMap = new HashMap<>();
        if(StringUtils.isNotBlank(orderBy)){
            List<String> orderList =  Arrays.asList(orderBy.split(";"));
            orderList.forEach(o -> {
                if (o.indexOf("asc") > 0) {
                    o.replaceAll(",asc", "");
                    List<String> ascList = Arrays.asList(o.split(","));
                    orderByMap.put("asc", ascList);
                } else if (o.indexOf("desc") > 0) {
                    o.replaceAll(",desc", "");
                    List<String> descList = Arrays.asList(o.split(","));
                    orderByMap.put("asc", descList);
                }
            });
        }
        if (orderByMap.size() > 0) {
            for (String key : orderByMap.keySet()) {
                if (key.equalsIgnoreCase("desc")) {
                    queryWrapper.orderByDesc(orderByMap.get(key));
                } else {
                    queryWrapper.orderByAsc(orderByMap.get(key));
                }
            }
        } else {
            List<String> columns = Arrays.asList(defaultOrderBy.split(","));
            queryWrapper.orderByDesc(columns);
        }
        return queryWrapper;
    }

    /**
     * 操作符转换
     *
     * @param condition 转换参数
     */
    public static String transformOperator(String condition) {
        if (StringUtils.isNotBlank(condition)) {
            Map<String, String> operatorType = getOperatorType();
            for (Map.Entry<String, String> entry : operatorType.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                condition = condition.replaceAll(key, value);
            }
        }
        return condition;
    }

    /**
     * 数学操作符与sql操作符映射
     * @return 返回操作数据
     */
    public static Map<String, String> getOperatorType() {
        Map<String, String> operatorMap = new HashMap<>();
        operatorMap.put("==", " = ");
        operatorMap.put("!=", " <> ");
        operatorMap.put("≠", " <> ");
        operatorMap.put("≥", " >= ");
        operatorMap.put("≤", " <= ");

        operatorMap.put("&&", " AND ");
        operatorMap.put("\\|\\|", " OR ");
        operatorMap.put("!", " NOT ");
        return operatorMap;
    }
}
